﻿/**
	$(CBI)* ...
	$(CBI)* @author Tophy 
	$(CBI)*/
	dynamic class D2.AngleVector 
	{
		private var _value:Number;
		private var _sign:Boolean;
		private var _start:Number;
		
		public static var roundAngle:Number = Math.PI * 2;
		//============================================================================================
		/**
		* 生成一个有起始弧度位置的角向量
		* @param	start 起始位置弧度
		* @param	end   结束位置弧度
		* @param	style 是否以劣弧方式生成向量(1),还是以优弧方式生成向量(2),或是不对优劣弧做判断(3)，默认为3
		* @return  返回一个角向量
		*/
		public static function From2Angle(start:Number, end:Number, style:Number):AngleVector {
			if (style == undefined) {
				style = 3;
			}
			var vm1:Number = ((end % roundAngle) + roundAngle) % roundAngle - ((start % roundAngle) + roundAngle) % roundAngle;
			var v:Number = end - start;
			style = Math.min(3, style);
			style = Math.max(1, style);
			
			if (style == 1) {
				if (Math.abs(vm1) < Math.PI) {
					return new AngleVector(start, vm1 > 0, Math.abs(vm1));
				}else {
					return new AngleVector(start, !(vm1 > 0), roundAngle - Math.abs(vm1));
				}
			}
			else if (style == 2) {
				if (Math.abs(vm1) > Math.PI) {
					return new AngleVector(start, vm1 > 0, Math.abs(vm1));
				}else {
					return new AngleVector(start, !(vm1 > 0), roundAngle - Math.abs(vm1));
				}
			}
			else if (style == 3) {
				return new AngleVector(start, v >= 0, v);
			}else {
				return null;
			}
		}
		//============================================================================================
		/**
		* 量
		*/
		public function get value():Number { return _value; }
		public function set value(value:Number):Void 
		{
			_value = value;
		}
		/**
		* 方向（顺＝true）
		*/
		public function get sign():Boolean { return _sign; }
		public function set sign(value:Boolean):Void 
		{
			_sign = value;
		}
		/**
		* 起始位置（弧度）
		*/
		public function get start():Number { return _start; }
		public function set start(value:Number):Void 
		{
			_start = value % roundAngle;
		}
		//============================================================================================
		
		public function AngleVector(start:Number,sign:Boolean,value:Number)
		{
			if (start == undefined) {
				start = 0;
			}
			if (sign == undefined) {
				sign = true;
			}
			if (value == undefined) {
				value = 0;
			}
			_sign = sign;
			_value = value;
			_start = start;
		}
		/**
		* 角向量加法
		* @param	av 被加向量
		* @return
		*/
		public function add(av:AngleVector):AngleVector {
			if (this._sign != av._sign) {
				this._value -= av._value;
			}else {
				this._value += av._value;
			}
			return this;
		}
		/**
		* 角向量减法
		* @param	av 减向量
		* @return
		*/
		public function sub(av:AngleVector):AngleVector {
			if (this._sign != av._sign) {
				this._value += av._value;
			}else {
				this._value -= av._value;
			}
			return this;
		}
		/**
		* 复制向量
		* @return
		*/
		public function clone():AngleVector {
			return new AngleVector(this._start, this._sign, this._value);
		}
		/**
		* 获得结束位置弧度值
		*/
		public function getEndAngle() {
			if (this._sign) {
				return (this._start + this._value) % roundAngle;
			}else {
				return (this._start - this._value) % roundAngle;
			}
		}
		
		public function toString():String 
		{
			return ((int(_start * 100)/100).toString()+(_sign? "+":"-")+(int(_value*100)/100).toString());
		}
	}
	